/**
 * Copyright (c) 2009 blurredVision
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT 
 * OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include "StdAfx.h"
#include "ISkyBox.h"
#include "core/core.h"

#ifdef CORE_DIRECTX
#include "dx/SkyBox.h"
#endif

ISkyBox::ISkyBox(void)
: 	pEffect(NULL),
	pCubeMap(NULL),
	pCubeMapBlend(NULL)
{
}

ISkyBox::~ISkyBox(void)
{
}

ISkyBox* ISkyBox::Create(const std::string &shader) 
{
	ISkyBox *res = NULL;

#ifdef CORE_DIRECTX
	res = new directx::SkyBox();
#endif

	if (shader == "") 
		res->pEffect = coreGetResourceFactory()->loadShaderEffect("media\\skybox_blend.fx");
	else
		res->pEffect = coreGetResourceFactory()->loadShaderEffect(shader);

	if (res->pEffect) {
		res->pEffect->setTechnique("Skybox");
		// Get the handles
		res->hInvWorldViewProjMat     = res->pEffect->getParameterByName("invWorldViewProjMat");
		res->hBlendFactor             = res->pEffect->getParameterByName("blendFactor");
		res->hEnvironmentTexture      = res->pEffect->getParameterByName("environmentTexture");
		res->hEnvironmentTextureBlend = res->pEffect->getParameterByName("environmentTextureBlend");
	}

	return res;
}

void ISkyBox::setCubeMap(ITexture *texture)
{
	pCubeMap = texture;
    // Draw the skybox
	pEffect->setTexture(hEnvironmentTexture, pCubeMap);
}

void ISkyBox::setCubeMapBlend(ITexture *texture)
{
	pCubeMapBlend = texture;
	pEffect->setTexture(hEnvironmentTextureBlend, pCubeMapBlend);
}

void ISkyBox::draw(math::Matrix4* reflMat, float blendFactor)
{
	// Get the inverse world view projection matrix 
	static math::Matrix4 invWorldViewProj;
	static IRenderer* pRenderer = coreGetRenderer();
	pRenderer->pushMatrix();

	if (reflMat)
		pRenderer->applyMatrix(*reflMat);

	invWorldViewProj = pRenderer->getWorldViewProjMatrix();
	invWorldViewProj.inv();

	pEffect->setMatrix(hInvWorldViewProjMat, &invWorldViewProj );
	
	if (pCubeMapBlend) {
		pEffect->setFloat(hBlendFactor, &blendFactor, 1);
	}

	// Bind the skybox effect
	if (pEffect->bind()) {
		// Render each pass
		for (int i=0; i<pEffect->getNumPasses(); ++i) {
			if (pEffect->beginPass(i)) {
				pEffect->setVariables();
				drawData();
				pEffect->endPass();
			}
		}
		// Unbind the effect
		pEffect->unbind();
	}

	pRenderer->popMatrix();
}
